/**
 * 
 */
package tp.pr5.instructions;

import tp.pr5.Constants;
import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.Item;
import tp.pr5.items.ItemContainer;

/**
 * The Instruction for using an item. This Instruction works if the user writes OPERATE id or OPERAR id
 *
 */
public class OperateInstruction implements Instruction {
	private RobotEngine engine;
	private NavigationModule navigation;
	private ItemContainer robotContainer;
	private String id;
	
	/**
	 * It builds a default OperateInstruction
	 */
	public OperateInstruction()
	{}
	
	public OperateInstruction(String id)
	{
		this.id = id;
	}

	@Override
	/**
	 * Parses the String returning an instance of OperateInstruction or throwing a WrongInstructionFormatException()
	 * @param cad - text String to parse
	 * @return Instruction Reference to an instance of PickInstruction
	 * @throws WrongInstructionFormatException - hen the String is not 
	 */
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		String[] commands;
		commands = cad.split(" ");
		
		if (commands[0].equalsIgnoreCase("OPERATE") || commands[0].equalsIgnoreCase("OPERAR"))
		{
			if (commands.length >=2)
			{
				return new OperateInstruction(commands[1]);
			}
			else
				throw new WrongInstructionFormatException();
		}
		else
			throw new WrongInstructionFormatException();
	}
	
	
	@Override
    /**
     * Returns a description of the Instruction syntax. The string does not end with the line separator. It is up to the caller adding it before printing.
     * @return The Instruction's syntax.
     */
	public String getHelp() {
		return "OPERATE|OPERAR <ID>";
	}

	@Override
	/**
	 * Set the execution context. The method receives the entire engine (engine, navigation and the robot container) even though the actual implementation of execute() may not require it.
	 * @param engine - The robot engine
	 * @param navigation - The information about the game, i.e., the places, current direction and current heading to navigate
	 * @param robotContainer - The inventory of the robot
	 */
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer) {
		this.engine = engine;
		this.navigation = navigation;
		this.robotContainer = robotContainer;
	}

	@Override
	/**
	 * The robot uses the requested item.
	 * @throws InstructionExecutionException - When the robot inventory does not contain an item with this name or when the item cannot be used
	 */
	public void execute() throws InstructionExecutionException {
		Item item = robotContainer.getItem(this.id); 
		if (item != null)
		{
			if (!item.use(this.engine, this.navigation))
			{
				throw new InstructionExecutionException(Constants.MESSAGE_SAYS +  Constants.MESSAGE_CHANGES_ERROR + this.id.toLowerCase());
			}
			if (!item.canBeUsed())
				this.robotContainer.useItem(item);
		}
		else
			throw new InstructionExecutionException();
	}
}
